package com.example.designsoft.base;

import android.util.Log;

import com.example.designsoft.MyApplication;

import java.lang.ref.WeakReference;

/**
 * author : Gyk
 * time   : 2020/11/17
 * todo   :
 */
public abstract class BasePresenter<M,V> {

    public M mModel;
    public V mView;
    public WeakReference<V> mWeakReference;

    /**
     * 绑定view与model
     *
     * @param pModel
     * @param pView
     */
    public void attachModelView(M pModel, V pView) {
        mWeakReference = new WeakReference<V>(pView);
        this.mModel = pModel;
    }

    /**
     * 获取view实例
     *
     * @return
     */
    public V getView() {
        if (isAttach()) {
            return mWeakReference.get();
        } else {
            return null;
        }
    }

    public boolean isAttach() {
        return mWeakReference != null && mWeakReference.get() != null;
    }

    /**
     * 解除Presenter与View绑定(通过清除弱引用，来达到让GC回收对象的目的，从而减少内存泄漏)
     */
    public void onDettach() {
        if (mWeakReference != null) {
            mWeakReference.clear();
            mWeakReference = null;
        }
    }

}
